package com.yoomiito.app.ui.my.remain

import android.widget.Toast
import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.umeng.socialize.bean.SHARE_MEDIA
import com.yoomiito.app.base.AppConfig
import com.yoomiito.app.base.BaseActPresenter
import com.yoomiito.app.model.my.ApplyBean
import com.yoomiito.app.model.my.MyUserCenterInfo
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.utils.NetworkUtils
import com.yoomiito.app.utils.wx.WXUtils

class MyRemainPresenter(appConfig: AppConfig) : BaseActPresenter<MyRemainActivity>(appConfig) {
    fun getIsAgent() {
        if (NetworkUtils.checkNetPermission(v)) {
            return
        }
//        YmApi.getYmService()
//                .moneyAgent
//                .compose(v.bindToLifecycle())
//                .compose(XApi.getApiTransformerData())
//                .compose(XApi.getScheduler())
//                .subscribe(object : ApiSubscriber<MyRemainMoneyInfo>() {
//                    override fun onSuccess(data: MyRemainMoneyInfo) {
//                        if (hasV())
//                            v.setIsAngent(data)
//                    }
//
//                    override fun onFail(error: ApiException) {
//                        YMToast.showText(error.msg)
//                    }
//                })
    }

    fun WXAuthorization() {
        WXUtils.wxAuthorization(v,SHARE_MEDIA.WEIXIN,object : WXUtils.OnAuthorizationListener{
            override fun onResult(access_token: String?, refresh_token: String?, openid: String?, unionid: String?) {
                bindWX(access_token!!, refresh_token!!, openid!!, unionid!!)
            }
        })
    }

    fun getUserCenterData(type:Int) {
        YmApi.getYmService()
                .userCenterInfo
                .compose<BaseModel<MyUserCenterInfo>>(v.bindToLifecycle<BaseModel<MyUserCenterInfo>>())
                .compose<MyUserCenterInfo>(XApi.getApiTransformerData<MyUserCenterInfo>())
                .compose<MyUserCenterInfo>(XApi.getScheduler<MyUserCenterInfo>())
                .subscribe(object : ApiSubscriber<MyUserCenterInfo?>(handleException()) {

                    override fun onSuccess(t: MyUserCenterInfo?) {
                        if(type == 1){
                            t?.money?.let { v.setRemainMoney(it) }
                        }else{
                            t?.mugwordMoney?.let { v.setRemainMoney(it) }
                        }
                    }

                    override fun onFail(error: ApiException) {

                    }
                })
    }

    private fun bindWX(access_token: String, refresh_token: String, openid: String, unionid: String) {
        YmApi.getYmService()
                .bindWx(access_token, refresh_token, openid, unionid)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose(XApi.getScheduler<BaseModel<*>>())
                .subscribe(object : ApiSubscriber<BaseModel<*>>(handleException()) {
                    override fun onSuccess(bingPhoneBeanBaseModel: BaseModel<*>) {
                        getUserInfo()
                        //                        if (type==MyRemainActivity.TYPE_1){
                        if (hasV())
                            v.bindWX()
                        //                        }else {
                        //                            getV().bindWX2();
                        //                        }
                    }

                    override fun onFail(error: ApiException) {

                    }
                })
    }


    public fun applyList( page :Int,type:Int) {
        YmApi.getYmService()
                .applyList(page,type)
                .compose(v.bindToLifecycle())
                .compose(XApi.getApiTransformerNoData<BaseModel<ApplyBean>>())
                .compose(XApi.getScheduler<BaseModel<ApplyBean>>())
                .subscribe(object : ApiSubscriber<BaseModel<ApplyBean>>(handleException()) {
                    override fun onSuccess(data: BaseModel<ApplyBean>) {
                        v.setData(data.data.data)



                    }

                    override fun onFail(error: ApiException) {

                    }
                })
    }

}
